<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control\trigger;

use builder\form\util\control\Control;

/**
 * 触发器控件
 * @author cleverstone
 * @since ym2.0
 */
class Trigger extends Control
{
    /**
     * @var array
     */
    private $_options = [
        'control' => 'trigger',
        'watchScope' => [],//Angular Watch Scope
        'where' => '',//Js Trigger Where
        'triggerItems' => [],//触发项
        'display' => true,//触发显示/隐藏
        'style' => '',
        'class' => '',
        'attribute' => '',
    ];

    /**
     * 触发条件
     * - 支持闭包
     * - 普通参数
     * @param string $formGroup
     * @param string $field
     * @param string $op
     * @param int|string|boolean|float $value
     * @return $this
     */
    public function when()
    {
        $args = func_get_args();
        if (is_callable($args[0])) {
            $this->_options['where'] .= '(';
            call_user_func($args[0], $this);
            $this->_options['where'] .= ')';
        } else {
            $where = new Where([
                'formGroup' => $args[0],
                'field' => $args[1],
                'op' => $args[2],
                'value' => $args[3],
            ]);

            $this->_options['where'] .= "{$where->toString()}";
            $this->_options['watchScope'][] = $where->getScope();
        }

        return $this;
    }

    /**
     * and 触发条件
     * - 支持闭包
     * - 普通参数
     * @param string $formGroup
     * @param string $field
     * @param string $op
     * @param int|string|boolean|float $value
     * @return $this
     */
    public function andWhen()
    {
        $args = func_get_args();
        $connectSymbol = !empty($this->_options['where']) && $this->_options['where'] != '(' ? ' && ' : '';
        if (is_callable($args[0])) {
            $this->_options['where'] .= $connectSymbol . '(';
            call_user_func($args[0], $this);
            $this->_options['where'] .= ')';
        } else {
            $where = new Where([
                'formGroup' => $args[0],
                'field' => $args[1],
                'op' => $args[2],
                'value' => $args[3],
            ]);

            $this->_options['where'] .= $connectSymbol . "{$where->toString()}";
            $this->_options['watchScope'][] = $where->getScope();
        }

        return $this;
    }

    /**
     * or 触发条件
     * - 支持闭包
     * - 普通参数
     * @param string $formGroup
     * @param string $field
     * @param string $op
     * @param int|string|boolean|float $value
     * @return $this
     */
    public function orWhen()
    {
        $args = func_get_args();
        $connectSymbol = !empty($this->_options['where']) && $this->_options['where'] != '(' ? ' || ' : '';
        if (is_callable($args[0])) {
            $this->_options['where'] .= $connectSymbol . '(';
            call_user_func($args[0], $this);
            $this->_options['where'] .= ')';
        } else {
            $where = new Where([
                'formGroup' => $args[0],
                'field' => $args[1],
                'op' => $args[2],
                'value' => $args[3],
            ]);

            $this->_options['where'] .= $connectSymbol . "{$where->toString()}";
            $this->_options['watchScope'][] = $where->getScope();
        }

        return $this;
    }

    /**
     * 设置触发项
     * @param array $triggerItems
     * @return $this
     */
    public function triggerItems(array $triggerItems)
    {
        $this->_options['triggerItems'] = $triggerItems;
        return $this;
    }

    /**
     * 触发显示/隐藏
     * @param bool $boolean
     * @return $this
     */
    public function display($boolean = true)
    {
        $this->_options['display'] = (bool)$boolean;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        return $this->_options;
    }
}